package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserFollowsService;
import com.example.demo.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.List;

@RestController
@RequestMapping("/follow")
public class UserFollowsController {
    @Autowired
    private UserFollowsService userFollowsService;

    @Autowired
    private UserService userService;
    @Autowired
    private MessageController messageController;
    // 关注用户
    @PostMapping("/{followedId}")
    public Map<String, Object> followUser(@PathVariable Integer followedId, HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        if (user == null) {
            return Map.of("code", 401, "message", "用户未登录", "data", null);
        }
        // 判断是否关注自己
        if (user.getId().intValue() == followedId) {
            return Map.of("code", 400, "message", "不能关注自己", "data", null);
        }

        // 调用消息发送接口
        messageController.sendMessage(null, "follow", request);
        return userFollowsService.followUser(user.getId().intValue(), followedId);
    }

    // 取消关注用户
    @DeleteMapping("/{followedId}")
    public Map<String, Object> unfollowUser(@PathVariable Integer followedId, HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        if (user == null) {
            return Map.of("code", 401, "message", "用户未登录", "data", null);
        }
        // 判断是否取消关注自己
        if (user.getId().intValue() == followedId) {
            return Map.of("code", 400, "message", "不能取消关注自己", "data", null);
        }
        return userFollowsService.unfollowUser(user.getId().intValue(), followedId);
    }

    // 获取关注者数量
    @GetMapping("/count/{userId}")
    public Map<String, Object> getFollowerCount(@PathVariable Integer userId) {
        int count = userFollowsService.getFollowerCount(userId);
        return Map.of("code", 200, "message", "successful", "data", count);
    }

    // 获取关注者列表
    @GetMapping("/followers/{userId}")
    public Map<String, Object> getFollowers(@PathVariable Long userId) {
        List<User> followers = userFollowsService.getFollowers(userId);
        return Map.of("code", 200, "message", "successful", "data", followers);
    }

    // 获取当前用户关注的人的数量
    @GetMapping("/followed/count")
    public Map<String, Object> getFollowedCount(HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        if (user == null) {
            return Map.of("code", 401, "message", "用户未登录", "data", null);
        }
        int count = userFollowsService.getFollowedCount(user.getId().intValue());
        return Map.of("code", 200, "message", "successful", "data", count);
    }

    // 获取当前用户关注的人的列表
    @GetMapping("/followed/list")
    public Map<String, Object> getFollowedUsers(HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        if (user == null) {
            return Map.of("code", 401, "message", "用户未登录", "data", null);
        }
        List<User> followedUsers = userFollowsService.getFollowedUsers(user.getId());
        return Map.of("code", 200, "message", "successful", "data", followedUsers);
    }
    @GetMapping("/status/{followedId}")
    public Map<String, Object> checkFollowStatus(@PathVariable Integer followedId, HttpServletRequest request) {
        User user = userService.getCurrentUser(request);
        if (user == null) {
            return Map.of("code", 401, "message", "用户未登录", "data", null);
        }
        return userFollowsService.checkFollowStatus(user.getId().intValue(), followedId);
    }
}
